package cn.hema.controller;

import cn.hema.mapper.BorrowerMapper;
import cn.hema.po.Borrower;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 借阅人的服务控制层
 */
@AllArgsConstructor
@RestController
@Slf4j
@RequestMapping("/borrowers")
public class BorrowerController {

    private final BorrowerMapper borrowerMapper;

    /**
     * 根据id查询借阅人信息
     */
    @GetMapping("/{id}")
    public Borrower getBorrower(@PathVariable Long id) {
        Borrower borrower = borrowerMapper.getBorrowerById(id);
        log.info("-------------OK /borrowers/{id}--------------------");
        return borrower;
    }

    /**
     * 查询所有借阅人信息
     */
    @GetMapping("")
    public List<Borrower> getAllBorrowers() {
        List<Borrower> borrowerList = borrowerMapper.getAllBorrowers();
        log.info("-------------OK getAllBorrowers--------------------");
        return borrowerList;
    }

    /**
     * 新增一个借阅人
     */
    @PostMapping
    public void createBorrower(@RequestBody Borrower borrower) {
        borrowerMapper.addBorrower(borrower);
    }

    /**
     * 更新一个借阅人信息
     */
    @PutMapping("/{id}")
    public void updateBorrower(@PathVariable Long id, @RequestBody Borrower borrower) {
        borrower.setId(id);  // 设置要更新的 ID
        borrowerMapper.updateBorrower(borrower);
    }

    /**
     * 根据id删除借阅人信息
     */
    @DeleteMapping("/{id}")
    public void deleteBorrower(@PathVariable Long id) {
        borrowerMapper.deleteBorrower(id);
    }
}
